let App = getApp();
Page({

  data: {
    active: 0,
    category:[],
    scrollTop:0,
    api_url: App.Domain,
    re_value:'',
    sel_type:"normal",
    page:1,
    isOrder:'1',
    receiving:"",
    shopList:[]
  },


  onLoad: function (options) {
    var that = this;
    //页面启动后 调取首页的数据
    that.setData({
      wxapp: wx.getStorageSync('wxapp')
    });
    App.wx_setcolor(that.data.wxapp);

    //获得分类数据
    App._get('category/Showlist', {}, function (result) {
      that.setData({
        category: result.data.categorydata
      })
      var listData = result.data.categorydata
      let defaultData =listData[0]
      let defaultId = listData[0].id
      let defaultIdArr = defaultId
      for(let i=0;i<defaultData.childlist.length;i++){
        defaultIdArr += String(","+defaultData.childlist[i].id)
      }
      that.shopList(defaultIdArr)
      console.log(result);
    });
      //是否再接单时间
      App._get('index/time', {}, function (result) {
        that.setData({
          receiving:result.data
        })
      });
  },

  onShow: function () {

  },
  //商品列表
  shopList(idArr){
    var that = this
    var data = {
      id: idArr,
      page: 1,
      types: this.data.sel_type, 
      name: '',
    }
    App._get('goods/category_list',data,function(res){
      if(res.code == 1){
        that.setData({
          shopList:  res.data.pagedata.data
        })
      }
    })
  },
  leftMneu: function(event) {
    var itemData = this.data.category[event.detail]
    var firstId =  this.data.category[event.detail].id
    var idArr = firstId
    for(let i=0;i<itemData.childlist.length;i++){
      idArr += String(","+itemData.childlist[i].id)
    }
    console.log(idArr)
    this.shopList(idArr)
    this.setData({
      active: event.detail
    })
  },
  onSearchchange: function (event) {
    this.setData({
      re_value: event.detail
    })
  },
  off(Event){
    console.log(Event)
  },
  //跳转商品详情
  getDetails:function(e){
    console.log(e)
    wx.navigateTo({
      url: '../goods/index?goods_id='+e.currentTarget.dataset.id
    })
  },
  // 加入购物车
  addCart:function(event){
    console.log(event)
    // var token = wx.getStorageSync('token') 
    // if(token == '' || !token){
    //   wx.navigateTo({
    //     url: '../login/index'
    //   })
    // }
   var id = event.currentTarget.dataset.id
   var index = event.currentTarget.dataset.index
   var that = this
   var data = {
     goods_id:id,
     goods_num:1,
     goods_sku_id:''
   }
   App._post('cart/add',data,function(res){
    if(res.code == 1){
      var num = that.data.shopList[index].cartnum
      var curNum = "shopList["+index+"].cartnum"
      that.setData({
        [curNum]:JSON.parse(num)+1
      })
    }
  })
  },
  onSearch: function (event) {
    console.log(this.data.re_value);
    //这里跳转到  搜索页
    wx.navigateTo({
      url: './list?rename=1&name=' + this.data.re_value
    })
  },
})